using ns0;
using ns4;
using System;
using System.Collections.Generic;
using System.Text;
namespace ns9
{
	public sealed class Code39Reader : OneDReader
	{
		private const string string_0 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%";
		private static readonly char[] char_0 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%".ToCharArray();
		private static readonly int[] int_2 = new int[]
		{
			52,
			289,
			97,
			352,
			49,
			304,
			112,
			37,
			292,
			100,
			265,
			73,
			328,
			25,
			280,
			88,
			13,
			268,
			76,
			28,
			259,
			67,
			322,
			19,
			274,
			82,
			7,
			262,
			70,
			22,
			385,
			193,
			448,
			145,
			400,
			208,
			133,
			388,
			196,
			148,
			168,
			162,
			138,
			42
		};
		private static readonly int int_3 = Code39Reader.int_2[39];
		private bool bool_0;
		private bool bool_1;
		public Code39Reader()
		{
			this.bool_0 = false;
			this.bool_1 = false;
		}
		public Code39Reader(bool usingCheckDigit)
		{
			this.bool_0 = usingCheckDigit;
			this.bool_1 = false;
		}
		public Code39Reader(bool usingCheckDigit, bool extendedMode)
		{
			this.bool_0 = usingCheckDigit;
			this.bool_1 = extendedMode;
		}
		public override Result decodeRow(int rowNumber, BitArray bitArray_0, Dictionary<object, object> hints)
		{
			int[] array = Code39Reader.smethod_2(bitArray_0);
			int num = array[1];
			int size = bitArray_0.Size;
			while (num < size && !bitArray_0.get_Renamed(num))
			{
				num++;
			}
			StringBuilder stringBuilder = new StringBuilder(20);
			int[] array2 = new int[9];
			int num3;
			while (true)
			{
				OneDReader.smethod_0(bitArray_0, num, array2);
				int num2 = Code39Reader.smethod_3(array2);
				if (num2 < 0)
				{
					break;
				}
				char c = Code39Reader.smethod_4(num2);
				stringBuilder.Append(c);
				num3 = num;
				for (int i = 0; i < array2.Length; i++)
				{
					num += array2[i];
				}
				while (num < size && !bitArray_0.get_Renamed(num))
				{
					num++;
				}
				if (c == '*')
				{
					goto IL_BB;
				}
			}
			throw ReaderException.Instance;
			IL_BB:
			stringBuilder.Remove(stringBuilder.get_Length() - 1, 1);
			int num4 = 0;
			for (int i = 0; i < array2.Length; i++)
			{
				num4 += array2[i];
			}
			int num5 = num - num3 - num4;
			if (num != size && num5 / 2 < num4)
			{
				throw ReaderException.Instance;
			}
			if (this.bool_0)
			{
				int num6 = stringBuilder.get_Length() - 1;
				int num7 = 0;
				for (int i = 0; i < num6; i++)
				{
					num7 += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%".IndexOf(stringBuilder.get_Chars(i));
				}
				if (num7 % 43 != "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%".IndexOf(stringBuilder.get_Chars(num6)))
				{
					throw ReaderException.Instance;
				}
				stringBuilder.Remove(num6, 1);
			}
			string text = stringBuilder.ToString();
			if (this.bool_1)
			{
				text = Code39Reader.smethod_5(text);
			}
			if (text.get_Length() == 0)
			{
				throw ReaderException.Instance;
			}
			float float_ = (float)(array[1] + array[0]) / 2f;
			float float_2 = (float)(num + num3) / 2f;
			return new Result(text, null, new ResultPoint[]
			{
				new ResultPoint(float_, (float)rowNumber),
				new ResultPoint(float_2, (float)rowNumber)
			}, BarcodeFormat.CODE_39);
		}
		private static int[] smethod_2(BitArray bitArray_0)
		{
			int size = bitArray_0.Size;
			int num = 0;
			while (num < size && !bitArray_0.get_Renamed(num))
			{
				num++;
			}
			int num2 = 0;
			int[] array = new int[9];
			int num3 = num;
			bool flag = false;
			int num4 = array.Length;
			for (int i = num; i < size; i++)
			{
				if (bitArray_0.get_Renamed(i) ^ flag)
				{
					array[num2]++;
				}
				else
				{
					if (num2 == num4 - 1)
					{
						if (Code39Reader.smethod_3(array) == Code39Reader.int_3 && bitArray_0.isRange(Math.Max(0, num3 - (i - num3) / 2), num3, false))
						{
							return new int[]
							{
								num3,
								i
							};
						}
						num3 += array[0] + array[1];
						for (int j = 2; j < num4; j++)
						{
							array[j - 2] = array[j];
						}
						array[num4 - 2] = 0;
						array[num4 - 1] = 0;
						num2--;
					}
					else
					{
						num2++;
					}
					array[num2] = 1;
					flag = !flag;
				}
			}
			throw ReaderException.Instance;
		}
		private static int smethod_3(int[] int_4)
		{
			int num = int_4.Length;
			int num2 = 0;
			int i;
			int num5;
			int num6;
			int num7;
			while (true)
			{
				int num3 = 2147483647;
				for (i = 0; i < num; i++)
				{
					int num4 = int_4[i];
					if (num4 < num3 && num4 > num2)
					{
						num3 = num4;
					}
				}
				num2 = num3;
				num5 = 0;
				num6 = 0;
				num7 = 0;
				for (i = 0; i < num; i++)
				{
					int num4 = int_4[i];
					if (int_4[i] > num2)
					{
						num7 |= 1 << num - 1 - i;
						num5++;
						num6 += num4;
					}
				}
				if (num5 == 3)
				{
					break;
				}
				if (num5 <= 3)
				{
					goto IL_D1;
				}
			}
			i = 0;
			int result;
			while (i < num && num5 > 0)
			{
				int num4 = int_4[i];
				if (int_4[i] > num2)
				{
					num5--;
					if (num4 << 1 >= num6)
					{
						result = -1;
						return result;
					}
				}
				i++;
			}
			result = num7;
			return result;
			IL_D1:
			result = -1;
			return result;
		}
		private static char smethod_4(int int_4)
		{
			for (int i = 0; i < Code39Reader.int_2.Length; i++)
			{
				if (Code39Reader.int_2[i] == int_4)
				{
					return Code39Reader.char_0[i];
				}
			}
			throw ReaderException.Instance;
		}
		private static string smethod_5(string string_1)
		{
			int length = string_1.get_Length();
			StringBuilder stringBuilder = new StringBuilder(length);
			for (int i = 0; i < length; i++)
			{
				char c = string_1.get_Chars(i);
				if (c == '+' || c == '$' || c == '%' || c == '/')
				{
					char c2 = string_1.get_Chars(i + 1);
					char c3 = '\0';
					char c4 = c;
					switch (c4)
					{
					case '$':
						if (c2 < 'A' || c2 > 'Z')
						{
							throw ReaderException.Instance;
						}
						c3 = c2 - '@';
						break;
					case '%':
						if (c2 >= 'A' && c2 <= 'E')
						{
							c3 = c2 - '&';
						}
						else
						{
							if (c2 < 'F' || c2 > 'W')
							{
								throw ReaderException.Instance;
							}
							c3 = c2 - '\v';
						}
						break;
					default:
						if (c4 != '+')
						{
							if (c4 == '/')
							{
								if (c2 >= 'A' && c2 <= 'O')
								{
									c3 = c2 - ' ';
								}
								else
								{
									if (c2 != 'Z')
									{
										throw ReaderException.Instance;
									}
									c3 = ':';
								}
							}
						}
						else
						{
							if (c2 < 'A' || c2 > 'Z')
							{
								throw ReaderException.Instance;
							}
							c3 = c2 + ' ';
						}
						break;
					}
					stringBuilder.Append(c3);
					i++;
				}
				else
				{
					stringBuilder.Append(c);
				}
			}
			return stringBuilder.ToString();
		}
	}
}
